﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Threading;
using PlanningGIS.MetaData;
using PlanningGIS.Win.Controls;
using System.Threading.Tasks;
using PlanningGIS.Util.Log;
using PlanningGIS.Framework;
using System.IO;

namespace PlanningGIS.Product
{
    /// <summary>
    /// 将产品数据由Blob转到FTP中
    /// </summary>
    public partial class FrmBlob2FTP : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmBlob2FTP()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmBlob2FTP_Load(object sender, EventArgs e)
        {
            _pgrsor = new Progressor() { ProgressBar = progressBarX1 };
            cmbRank.ValueMember = "Value";
            cmbRank.DisplayMember = "Name";
            cmbRank.DataSource = MetaDataType.GetRanks(ProductHelper.ProductConfig.DataTypes);
            cmbRank.SelectedIndex = 0;
        }

        /// <summary>
        /// 进度条
        /// </summary>
        private Progressor _pgrsor = null;

        /// <summary>
        /// 选择数据等级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbRank_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbDataType.ValueMember = "TableName";
            cmbDataType.DisplayMember = "DataType";
            cmbDataType.DataSource = MetaDataType.GetDataTypes(ProductHelper.ProductConfig.DataTypes, cmbRank.SelectedValue.ToString());
            cmbDataType.SelectedIndex = 0;
        }

        /// <summary>
        /// 批量选择改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkPatch_CheckedChanged(object sender, EventArgs e)
        {
            cmbDataType.Enabled = !chkPatch.Checked;
            cmbRank.Enabled = !chkPatch.Checked;
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (MsgHelper.ShowYesOrNoQueryMsgBox("该操作耗时较长，是否确定进行该操作？") != System.Windows.Forms.DialogResult.Yes) return;
            if (string.IsNullOrWhiteSpace(AppConfig.SystemConfig.FTP.LocalPath))
            {
                MsgHelper.ShowErrorMsgbox(this, "FTP配置的路径为空，请先配置FTp的路径！");
                return;
            }
            if (!Directory.Exists(AppConfig.SystemConfig.FTP.LocalPath))
                Directory.CreateDirectory(AppConfig.SystemConfig.FTP.LocalPath);
            btnOK.Enabled = false;
            List<MetaDataType> datatypes = ProductHelper.ProductConfig.DataTypes;
            if (!chkPatch.Checked)
                datatypes = new List<MetaDataType>() { (MetaDataType)cmbDataType.SelectedItem };
            //_thread = new Thread(new ThreadStart(DoExport));
            //_thread.IsBackground = true;
            //_thread.Start();            
            DoExport(datatypes);
        }

        /// <summary>
        /// 开始导出
        /// </summary>
        private void DoExport(List<MetaDataType> datatypes)
        {
            foreach (MetaDataType m in datatypes)
            {
                try
                {
                    string sql = "select id," + MetaHelper.FindName(m.Rank.ToString(), MetaHelper.KEY);
                    sql += " as th," + MetaHelper.BLCFM + " as blcfm," + MetaHelper.SCRQ + " as scrq ";
                    sql += " from " + m.TableName;
                    LogHelper.WriteLog("开始获取数据：" + m.TableName);
                    DataTable dt = ProductHelper.DbConnect.Context.GetDataTable(sql);
                    if (dt == null || dt.Rows.Count == 0)
                    {
                        LogHelper.WriteLog(m.TableName + "数据为空", zjgisLogType.zjgisLogDone);
                        continue;
                    }
                    _pgrsor.SetMaxValue(dt.Rows.Count);
                    foreach (DataRow dr in dt.Rows)
                    {
                        try
                        {
                            _pgrsor.Step();
                            string id = dr["id"].ToString();
                            LogHelper.WriteLog("开始转换" + id);
                            string path = ViewD4Helper.GetProductName(m, DateTime.Parse(dr["scrq"].ToString()), dr["blcfm"].ToString(), "");
                            path = AppConfig.SystemConfig.FTP.LocalPath + "\\" + path;

                            ViewD4Helper.ExportProduct(path, id, null, dr["th"].ToString());
                            ViewD4Helper.ExportMetaXml(path, m.TableName, id, dr["th"].ToString());
                            LogHelper.WriteLog(id + "转换完成", zjgisLogType.zjgisLogDone);
                            GC.Collect();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.WriteLog(ex);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog(ex);
                }                
            }

            
        }

        /// <summary>
        /// 完成操作
        /// </summary>
        private void Finished()
        {
            btnOK.Enabled = true;
            this.Cursor = Cursors.Default;
        }


        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
           

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
